<template>
  <div class="container">
    <div class="list"
         @click="select_address(item)"
         @longTap="delete_item(item)"
         :actions="actions"
         v-for="(item, index) in address_list"
         :key="index">
      <view slot="content">
        <span class="rec-name">{{item.receive_name}}</span>
        <div class="tel">{{item.phone}}</div>
        <div class="tip"
             v-if="item.is_default==1">默认</div>
        <div class="addr-msg">
          {{item.detail_address}} {{item.address}}
        </div>
        <div class="rewrite"
             @tap.stop="rewrite_address(item)">
          <img :src="base_url+'/mp_2.0/wode/bianji@2x.png'"
               v-if="base_url"
               alt=""
               class="img">
        </div>
      </view>
    </div>
    <a class="add-address"
       href="/pages/package_mine/write_address/main">
      新增收货地址
    </a>
    <div class="empty"
         v-if="isempty">
      <img src="https://img.ameimeika.com/h5_images/mp_images/mp_3.0/search/noaddress.png"
           v-if="base_url"
           alt=""
           class="nothing">
      <div class="text">一个地址都没有哦~</div>
    </div>
  </div>
</template>
<script>
import common from "@/assets/js/mmk_common.js";
export default {
  data () {
    return {
      base_url: common.image_resource,
      province_arr: [],
      province_id: "",
      city_arr: [],
      city_id: "",
      area_arr: [],
      area_id: "",
      address_list: [],
      select_status: false,
      isempty: false,
      actions: [
        {
          name: "删除",
          color: "#fff",
          fontsize: "14",
          width: 74,
          icon: "none",
          background: "#FF3B30"
        }
      ],
      toisPlus: false,
      cardId: "",
      address_option: {}
    };
  },
  methods: {
    delete_item (item) {
      let that = this;
      wx.showLoading({
        title: "删除中...", //提示的内容,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => {
          common.fly_post("api/v4_2/user_address/del", {
            id: item.id
          }, result => {
            wx.hideLoading();
            let res = result.data;
            if (res.status_code == 0) {
              wx.showToast({
                title: "删除成功", //提示的内容,
                icon: "success", //图标,
                duration: 2000, //延迟时间,
                mask: true, //显示透明蒙层，防止触摸穿透,
                success: res => {
                  setTimeout(() => {
                    that.get_list();
                  }, 1500);
                }
              });
            } else {
              wx.showToast({
                title: res.message,
                icon: "none",
                duration: 2000,
                mask: false
              });
            }
          })
        }
      });
    },
    get_list () {
      common.mmk_Loading(0)
      common.fly_post("api/v4_2/user_address/index", {
        page: 1,
        pageSize: 1000
      }, result => {
        common.mmk_Loading(1)
        let res = result.data;
        if (res.status_code == 0) {
          if (res.data.length == 0) {
            this.isempty = true;
          } else {
            this.isempty = false;
            this.address_list = res.data.data;
          }
        } else {
          common.mmk_Loading(2, res.message)
        }
      })
    },
    select_address (item) {
        console.log(this.toisPlus);
        
      if (this.toisPlus) {
        let addressJson = JSON.stringify(item)
        let router = `${common.life_beauty}order?id=${this.cardId}&isPlus=true&addressJson=${addressJson}&goods_json=${this.address_option.goods_json || ''}&beauty_coupon_ids=${this.address_option.beauty_coupon_ids || ''}&goods_coupon_ids=${this.address_option.goods_coupon_ids || ''}`
        wx.redirectTo({
          url: `/pages/package_main/advertisement/main?url=` + encodeURIComponent(router)
        })
      }
      wx.setStorageSync("address_info", item);
      if (this.select_status) {
        wx.navigateBack({
          delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
        });
      }
    },
    rewrite_address (item) {
      wx.setStorageSync("rewrite_data", item);
      wx.navigateTo({ url: "/pages/package_mine/rewrite_address/main" });
    }
  },
  onLoad (options) {
    wx.setNavigationBarTitle({
      title: "收货地址"
    });
    if (options.toisPlus) {
      this.toisPlus = options.toisPlus
    }
    this.cardId = options.cardId ? options.cardId : ""
    this.address_option = options || {}
    // 如果传过来有status，就是选择地址
    if (options.status) {
      this.select_status = true;
    }
  },
  onShow () {
    this.get_list();
  }
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  background: rgba(243, 244, 246, 1);
  .list {
    background-color: #fff;
    padding: 10px;
    .rec-name {
      display: inline-block;
      font-size: 14px;
      font-family: "Medium";
      font-weight: bold;
      color: rgba(68, 68, 68, 1);
      margin-right: 10px;
      line-height: 20px;
      vertical-align: middle;
    }
    .tel {
      display: inline-block;
      font-size: 14px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(102, 102, 102, 1);
      line-height: 20px;
      vertical-align: middle;
    }
    .tip {
      width: 40px;
      height: 20px;
      display: inline-block;
      background: rgba(255, 134, 105, 1);
      border-radius: 20px;
      line-height: 20px;
      text-align: center;
      font-size: 10px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(255, 255, 255, 1);
      margin-left: 10px;
      vertical-align: middle;
    }
    .addr-msg {
      font-size: 14px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(68, 68, 68, 1);
      margin-top: 6px;
    }
    .rewrite {
      width: 20px;
      height: 20px;
      position: absolute;
      top: 15px;
      right: 15px;
      .img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .add-address {
    width: 100%;
    height: 50px;
    background: rgba(255, 56, 62, 1);
    font-size: 17px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    line-height: 50px;
    text-align: center;
    position: fixed;
    bottom: 0px;
  }
  .empty {
    width: 154px;
    position: fixed;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
    .nothing {
      width: 128px;
      height: 114px;
      margin: 0 auto;
      display: block;
    }
    .text {
      width: 100%;
      font-size: 14px;
      font-weight: 400;
      margin-top: 13px;
      color: rgba(153, 153, 153, 1);
      text-align: center;
    }
  }
}
</style>

